/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.service;

import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_CHANGE_COUNT;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_CHANGE_DURATION;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_END;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_END_LAT;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_END_LNG;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_END_SELECTION_TYPE;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_IS_ARRIVAL_IND;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_SCHEDULE_ID;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_SCHEDULE_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_SCHEDULE_TRACK_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_SCOPE_FOLDER;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_SCOPE_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_START;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_START_LAT;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_START_LNG;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_START_SELECTION_TYPE;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_STOP_DIRECTION_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_STOP_ID;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_STOP_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_TRACK_ID;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_TRACK_NAME;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_TRANSPORT_ID;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_TRANSPORT_TYPES;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_WALK_DISTANCE;
import static lt.marsrutai.mobile.android.service.BookmarkColumns.FIELD_WALK_SPEED;
import static lt.marsrutai.mobile.android.service.EntityColumns.FIELD_ID;
import static lt.marsrutai.mobile.android.service.EntityColumns.FIELD_TYPE;
import lt.marsrutai.mobile.android.model.Bookmark;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class BookmarkService extends AbstractObjectStoreSQLiteHelper<Bookmark> {

    private static final String TABLE_NAME = "Bookmarks";
    private static final int DATABASE_VERSION = 4;

    private static final ColumnDefinition[] COLUMNS = new ColumnDefinition[] {
        ColumnDefinition.of(FIELD_ID, ColumnTypes.INT, "PRIMARY KEY AUTOINCREMENT"),
        ColumnDefinition.of(FIELD_TYPE, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_SCOPE_FOLDER, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_SCOPE_NAME, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_STOP_ID, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_STOP_NAME, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_STOP_DIRECTION_NAME, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_TRANSPORT_ID, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_SCHEDULE_ID, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_SCHEDULE_NAME, ColumnTypes.TEXT), 
        ColumnDefinition.of(FIELD_SCHEDULE_TRACK_NAME, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_TRACK_ID, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_TRACK_NAME, ColumnTypes.TEXT),
            // v2
        ColumnDefinition.of(FIELD_START, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_END, ColumnTypes.TEXT),
            // v3
        ColumnDefinition.of(FIELD_NAME, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_START_LAT, ColumnTypes.REAL),
        ColumnDefinition.of(FIELD_START_LNG, ColumnTypes.REAL),
        ColumnDefinition.of(FIELD_START_SELECTION_TYPE, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_END_LAT, ColumnTypes.REAL),
        ColumnDefinition.of(FIELD_END_LNG, ColumnTypes.REAL),
        ColumnDefinition.of(FIELD_END_SELECTION_TYPE, ColumnTypes.TEXT),
        ColumnDefinition.of(FIELD_IS_ARRIVAL_IND, ColumnTypes.INT),
            // v4
        ColumnDefinition.of(FIELD_CHANGE_COUNT, ColumnTypes.INT),
        ColumnDefinition.of(FIELD_CHANGE_DURATION, ColumnTypes.INT),
        ColumnDefinition.of(FIELD_WALK_DISTANCE, ColumnTypes.INT),
        ColumnDefinition.of(FIELD_WALK_SPEED, ColumnTypes.INT),
        ColumnDefinition.of(FIELD_TRANSPORT_TYPES, ColumnTypes.TEXT)
    };

    public BookmarkService(Context context) {
        super(context, TABLE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(getCreateTableQuery());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            addColumn(db, FIELD_START, ColumnTypes.TEXT);
            addColumn(db, FIELD_END, ColumnTypes.TEXT);
        }
        if (oldVersion < 3) {
            addColumn(db, FIELD_NAME, ColumnTypes.TEXT);
            addColumn(db, FIELD_START_LAT, ColumnTypes.REAL);
            addColumn(db, FIELD_START_LNG, ColumnTypes.REAL);
            addColumn(db, FIELD_START_SELECTION_TYPE, ColumnTypes.TEXT);
            addColumn(db, FIELD_END_LAT, ColumnTypes.REAL);
            addColumn(db, FIELD_END_LNG, ColumnTypes.REAL);
            addColumn(db, FIELD_END_SELECTION_TYPE, ColumnTypes.TEXT);
            addColumn(db, FIELD_IS_ARRIVAL_IND, ColumnTypes.INT);
        }
        if (oldVersion < 4) {
            addColumn(db, FIELD_CHANGE_COUNT, ColumnTypes.INT);
            addColumn(db, FIELD_CHANGE_DURATION, ColumnTypes.INT);
            addColumn(db, FIELD_WALK_DISTANCE, ColumnTypes.INT);
            addColumn(db, FIELD_WALK_SPEED, ColumnTypes.INT);
            addColumn(db, FIELD_TRANSPORT_TYPES, ColumnTypes.TEXT);
        }
    }

    @Override
    protected String getTableName() {
        return TABLE_NAME;
    }

    @Override
    protected ColumnDefinition[] getColumnDefinitions() {
        return COLUMNS;
    }
}
